package gui.popupmenu;

import gui.image.FolderImageList;
import gui.image.ImagePanel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
import org.apache.commons.io.FileUtils;
import util.Clipboard;

/**
 *
 * @author Quân
 */
public class PasteMouseAdaptor extends MouseAdapter {

    private FolderImageList folderImageList;

    public PasteMouseAdaptor(FolderImageList folderImageList) {
        this.folderImageList = folderImageList;
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        Clipboard clipboard = util.Clipboard.getInstance();
        File des = folderImageList.getFile();
        ImagePanel imagePanel = clipboard.getData();
        try {
            if (clipboard.getType() == Clipboard.COPY) {
                FileUtils.copyFile(imagePanel.getFile(),
                        new File(des, imagePanel.getFile().getName()));
            } else {
                JPanel jPanel = (JPanel) imagePanel.getParent();
                jPanel.remove(imagePanel);
                jPanel.updateUI();
                FileUtils.moveFile(imagePanel.getFile(),
                        new File(des, imagePanel.getFile().getName()));
            }
            folderImageList.getjPanelImage().add(new ImagePanel(new File(des, imagePanel.getFile().getName())));
            folderImageList.updateUI();
            util.Clipboard.getInstance().setClipboard(null);
        } catch (IOException ex) {
            Logger.getLogger(PasteMouseAdaptor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
